package blog.db;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import blog.util.StringUtils;

public class ShortCut implements IStorableRecordWithIndex {
	
	public static final int FIELD_CONTENT = 1;
	public static final int FIELD_VISIT_COUNT = 2;
	
	public String content;
	public int visitCount;
	
	private int m_id;
	private int m_indexId;
	
	public ShortCut()
	{
		content = "";
		visitCount = 0;
		m_id = -1;
		m_indexId = -1;
	}

	public int compare(int field, IStorableRecordWithIndex another) {
		ShortCut tmpObj = (ShortCut) another;
		switch( field )
		{
		case FIELD_CONTENT:
		{
			return content.compareTo(tmpObj.content);
		}
		case FIELD_VISIT_COUNT:
		{
			return this.visitCount - tmpObj.visitCount;
		}
		}
		return 0;
	}

	public int compareField(int field, Object fieldValue) {
		switch( field )
		{
		case FIELD_CONTENT:
		{
			return content.compareTo((String)fieldValue);
		}
		case FIELD_VISIT_COUNT:
		{
			return this.visitCount - ((Integer)fieldValue).intValue();
		}
		}
		return 0;
	}

	public void deserializeIndex(DataInputStream dis) throws IOException {
		this.content = dis.readUTF();
		this.visitCount = dis.readInt();
	}

	public int getIndexId() {
		return m_indexId;
	}

	public boolean isIndexField(int field) {//ALL FIELDS ARE INDEX FIELD.
		return true;
	}

	public void serializeIndex(DataOutputStream dos) throws IOException {
		dos.writeUTF( StringUtils.null2empty( this.content ) );
		dos.writeInt( this.visitCount );
	}

	public void setIndexId(int indexId) {
		this.m_indexId = indexId;
	}

	public void deserialize(DataInputStream dis) throws IOException {
		this.deserializeIndex(dis);
	}

	public int getId() {
		return m_id;
	}

	public void serialize(DataOutputStream dos) throws IOException {
		this.serializeIndex(dos);
	}

	public void setId(int id) {
		m_id = id;
	}

}